package com.wxg.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <a href="https://baijiahao.baidu.com/s?id=1630163163382081801&wfr=spider&for=pc">大厂不建议使用 Excutors 创建线程池</a>
 */
public class UseThreadPoolRightWay {
    public static void main(String[] args) {
        // 大厂不建议使用 Excutors 创建线程池

        ThreadFactory threadFactory = new ThreadFactory() {
            private AtomicLong atomicLong = new AtomicLong(1);

            @Override
            public Thread newThread(Runnable r) {
                String threadName = String.format("MyThread-%d-", atomicLong.getAndIncrement());
                Thread thread = new Thread(r, threadName);
                return thread;
            }
        };

        ExecutorService threadPool = new ThreadPoolExecutor(5, 200,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), threadFactory,
                new ThreadPoolExecutor.AbortPolicy());
    }
}
